/* ** por compatibilidad se omiten tildes **
================================================================================
 TRABAJO PRACTICO 3 - System Programming - ORGANIZACION DE COMPUTADOR II - FCEN
================================================================================
  definicion de la tabla de descriptores globales
*/

#include "gdt.h"

gdt_entry gdt[GDT_COUNT] = {
    /* Descriptor nulo*/
    /* Offset = 0x00 */
    [GDT_IDX_NULL_DESC] = (gdt_entry) {
        (unsigned short)    0x0000,         /* limit[0:15]  */
        (unsigned short)    0x0000,         /* base[0:15]   */
        (unsigned char)     0x00,           /* base[23:16]  */
        (unsigned char)     0x00,           /* type         */
        (unsigned char)     0x00,           /* s            */
        (unsigned char)     0x00,           /* dpl          */
        (unsigned char)     0x00,           /* p            */
        (unsigned char)     0x00,           /* limit[16:19] */
        (unsigned char)     0x00,           /* avl          */
        (unsigned char)     0x00,           /* l            */
        (unsigned char)     0x00,           /* db           */
        (unsigned char)     0x00,           /* g            */
        (unsigned char)     0x00,           /* base[31:24]  */
    },
    [GDT_IDX_CODE_0] = (gdt_entry) {
        (unsigned short)    0x6EFF,         /* limit[0:15]  */
        (unsigned short)    0x0000,         /* base[0:15]   */
        (unsigned char)     0x00,           /* base[23:16]  */
        (unsigned char)     0x0A,           /* type  R/E    */
        (unsigned char)     0x01,           /* s            */
        (unsigned char)     0x00,           /* dpl          */	
        (unsigned char)     0x01,           /* p            */
        (unsigned char)     0x02,           /* limit[16:19] */
        (unsigned char)     0x00,           /* avl          */
        (unsigned char)     0x00,           /* l            */
        (unsigned char)     0x01,           /* db           */
        (unsigned char)     0x01,           /* g            */
        (unsigned char)     0x00,           /* base[31:24]  */
    },
  //
  // Descriptor segmento de código de nivel (privilegio) 3.
  //
   [GDT_IDX_CODE_3] = (gdt_entry) {
        (unsigned short)    0x6EFF,         /* limit[0:15]  */
        (unsigned short)    0x0000,         /* base[0:15]   */
        (unsigned char)     0x00,           /* base[23:16]  */
        (unsigned char)     0x0A,           /* type   R/E   */
        (unsigned char)     0x01,           /* s            */
        (unsigned char)     0x03,           /* dpl          */
        (unsigned char)     0x01,           /* p            */
        (unsigned char)     0x02,           /* limit[16:19] */
        (unsigned char)     0x00,           /* avl          */
        (unsigned char)     0x00,           /* l            */
        (unsigned char)     0x01,           /* db           */
        (unsigned char)     0x01,           /* g            */
        (unsigned char)     0x00,           /* base[31:24]  */
    },
  //
  // Descriptor segmento de datos de nivel (privilegio) 0.
  //
    [GDT_IDX_DATA_0] = (gdt_entry) {
        (unsigned short)    0x6EFF,         /* limit[0:15]  */
        (unsigned short)    0x0000,         /* base[0:15]   */
        (unsigned char)     0x00,           /* base[23:16]  */
        (unsigned char)     0x02,           /* type  (0x2: Data - Read/Write)     */       
        (unsigned char)     0x01,           /* s            */
        (unsigned char)     0x00,           /* dpl          */
        (unsigned char)     0x01,           /* p            */
        (unsigned char)     0x02,           /* limit[16:19] */
        (unsigned char)     0x00,           /* avl          */
        (unsigned char)     0x00,           /* l            */
        (unsigned char)     0x01,           /* db           */
        (unsigned char)     0x01,           /* g            */
        (unsigned char)     0x00,           /* base[31:24]  */
    },
  //
  // Descriptor segmento de datos de nivel (privilegio) 3.
  //
   [GDT_IDX_DATA_3] = (gdt_entry) {
        (unsigned short)    0x6EFF,         /* limit[0:15]  */
        (unsigned short)    0x0000,         /* base[0:15]   */
        (unsigned char)     0x00,           /* base[23:16]  */
        (unsigned char)     0x02,           /* type    (0x2: Data - Read/Write)     */
        (unsigned char)     0x01,           /* s            */
        (unsigned char)     0x03,           /* dpl          */
        (unsigned char)     0x01,           /* p            */
        (unsigned char)     0x02,           /* limit[16:19] */
        (unsigned char)     0x00,           /* avl          */
        (unsigned char)     0x00,           /* l            */
        (unsigned char)     0x01,           /* db           */
        (unsigned char)     0x01,           /* g            */
        (unsigned char)     0x00,           /* base[31:24]  */
    },
  //
  // Descriptor de video: segmento de datos de nivel 0 para el video (empieza en 
  // 0xB8000).
  //
   [GDT_IDX_VIDEO] = (gdt_entry) {
        (unsigned short)    0x1F3F,         /* limit[0:15] 8000=2x80x50 */
        (unsigned short)    0x8000,         /* base[0:15]   */
        (unsigned char)     0x0B,           /* base[23:16]  */
        (unsigned char)     0x02,           /* type         */
        (unsigned char)     0x01,           /* s            */
        (unsigned char)     0x00,           /* dpl          */
        (unsigned char)     0x01,           /* p            */
        (unsigned char)     0x00,           /* limit[16:19] */
        (unsigned char)     0x00,           /* avl          */
        (unsigned char)     0x00,           /* l            */
        (unsigned char)     0x01,           /* db           */
        (unsigned char)     0x00,           /* g            */
        (unsigned char)     0x00,           /* base[31:24]  */
    },
  [GDT_IDX_INIT] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Descriptor Privilege Level (0: system)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  //
  // TSS 1: segmento de estado para la Tarea Idle.
  //
  [GDT_IDX_IDLE] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Descriptor Privilege Level (0: system)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_1] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_2] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_3] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_4] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_5] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_6] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_7] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_ROJO_8] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_1] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_2] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_3] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_4] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_5] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_6] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_7] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  },
  [GDT_IDX_AZUL_8] = (gdt_entry){ 
    (unsigned short) 0x0067, // Segment limit  0:15
    (unsigned short) 0x0000, // Base address   0:15
    (unsigned char)  0x00,   // Base address  15:23
    (unsigned char)  0x9,    // Type: siempre vale 0x9
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  3,      // Descriptor Privilege Level (3: user)
    (unsigned char)  1,      // Segment Present (1: true; 0: false).
    (unsigned char)  0x0,    // Segment limit  16:19
    (unsigned char)  1,      // AVL (Available for use by system software)
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Siempre vale 0.
    (unsigned char)  0,      // Granularity (0: byte units; 1: 4KB unit).
    (unsigned char)  0x00    // Base address  24:31
  }

 
};

gdt_descriptor GDT_DESC = {
    sizeof(gdt) - 1,
    (unsigned int) &gdt
};
